//
//  TheThirdCall.m
//  ITHome
//
//  Created by lanou3g on 15/7/20.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "TheThirdCall.h"
#import "UserInfoModel.h"
#import "WeiboUser.h"
#import "UMSocialSnsPlatformManager.h"
#import "UMSocialAccountManager.h"

@implementation TheThirdCall

#pragma mark - 单例
+ (TheThirdCall *)theThirdCallShare
{
    static TheThirdCall *tehThirdCall = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tehThirdCall = [[TheThirdCall alloc] init];
    });
    return tehThirdCall;
}

#pragma mark - 初始化
- (id)init
{
    // 创建TencentOauth 对象 并初始化
    NSString *appid = kTencentAppid;
    self.tencentOauth = [[TencentOAuth alloc] initWithAppId:appid andDelegate:self];
    
    return self;
}

#pragma mark - 登陆成功
- (void)tencentDidLogin
{
    [self.tencentOauth getUserInfo]; // 获取用户信息
}

#pragma mark - 登陆失败
- (void)tencentDidNotLogin:(BOOL)cancelled;
{
//    YAXLog(@"登陆失败");
}

#pragma mark - 登录时网络有问题的回调
- (void)tencentDidNotNetWork
{
//    YAXLog(@"没有网络");
}

#pragma mark - 获取QQ用户信息
- (void)getUserInfoResponse:(APIResponse*) response
{
    NSDictionary *userInfo = response.jsonResponse; // 获取用户信息
    

    
    UserInfoModel *userInfoModel = [UserInfoModel userInfoModelWithDictionary:userInfo];

    self.avatarImageUrl = [userInfo objectForKey:@"figureurl_qq_2"]; // 头像url
    self.userName = userInfoModel.nickname; //用户名
    
    // 创建用户表
    [FMDBManage createUserInfoTable];
    
    // 先删除数据库里面的内容
    [FMDBManage deleteAllUserInfo];
    
    // 插入数据
    BOOL flag = [FMDBManage insertUserInfoOpenId:userInfoModel.openId city:userInfoModel.city figureurl_qq_1:userInfoModel.figureurl_qq_1 figureurl_qq_2:userInfoModel.figureurl_qq_2 gender:userInfoModel.gender province:userInfoModel.province nickname:userInfoModel.nickname indirect:nil];
    
    // 插入数据成功后才记录用户登陆信息
    if (flag)
    {
        // 保存登陆状态
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:@"YES" forKey:kLoginStatus];
        [userDefaults synchronize]; // 同步到磁盘中
        
        // 创建一个通知·· 用户登陆状态的通知,让相关页面刷新数据
        [[NSNotificationCenter defaultCenter] postNotificationName:kLoginSuccessed object:userInfoModel.figureurl_qq_1];
    }
    else
    {
        // 什么都不做
    }
}


#pragma mark - 获取新浪微博用户信息
- (void)getSinaUserInfo
{
    // 拼接url 获取用户数据
    NSString *url = [NSString stringWithFormat:@"https://api.weibo.com/2/users/show.json?access_token=%@&uid=%@",self.wbtoken, self.wbCurrentUserID];
    // 解析数据
    [LORequestManger GET:url success:^(id response) {
        // 解析
        WeiboUser *weiUserModle = [WeiboUser userWithDictionary:(NSDictionary *)response];
        weiUserModle.userDescription = [(NSDictionary *)response objectForKey:@"description"];
        
        self.avatarImageUrl = weiUserModle.avatarLargeUrl; // 头像url
        self.userName = weiUserModle.screenName; // 用户名
        
        // 按空格分隔省份 和城市 如  “福建 福州”
        NSArray *array = [weiUserModle.location componentsSeparatedByString:@" "];
        
        
        // 把图片转换成uimage
        NSData *imageData_avatar_40 = [NSData dataWithContentsOfURL:[NSURL URLWithString:weiUserModle.avatarLargeUrl]];
        UIImage *figureurl_avatar_1 = [UIImage imageWithData:imageData_avatar_40];
        
        NSData *imageData_avatar_100 = [NSData dataWithContentsOfURL:[NSURL URLWithString:weiUserModle.avatarHDUrl]];
        UIImage *figureurl_avatar_2 = [UIImage imageWithData:imageData_avatar_100];
        
        // 创建用户表
        [FMDBManage createUserInfoTable];
        
        // 插入数据
        // 先删除数据库里面的内容
        [FMDBManage deleteAllUserInfo];
        
        // 插入数据
        BOOL flag = [FMDBManage insertUserInfoOpenId:weiUserModle.userID city:array[1] figureurl_qq_1:figureurl_avatar_1 figureurl_qq_2:figureurl_avatar_2 gender:weiUserModle.gender province:array[0] nickname:weiUserModle.screenName indirect:weiUserModle.userDescription];
        
        // 插入数据成功后才记录用户登陆信息
        if (flag)
        {
            // 保存登陆状态
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:@"YES" forKey:kLoginStatus];
            [userDefaults synchronize]; // 同步到磁盘中
            
            // 创建一个通知·· 用户登陆状态的通知,让相关页面刷新数据
            [[NSNotificationCenter defaultCenter] postNotificationName:kLoginSuccessed object:figureurl_avatar_2];
        }
        else
        {
            // 什么都不做
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
@end
